package com.qf.air.filter;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 10537
 * @version V1.0    zuul限流操作
 * @Date 2021/11/12 19:49
 */
@Component
public class Ratelimiter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1500;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    private RateLimiter rateLimiter=RateLimiter.create(1);

    @Override
    public Object run() throws ZuulException {
        System.out.println(new Date());
        if (rateLimiter.tryAcquire()){
            System.out.println("获取成功");
        }else {
            System.out.println("为获取到钥匙，无法进入");
        }
        return null;
    }
}
